import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import ServerUrlCopy from 'vite-plugin-url-copy'
import { viteMockServe } from 'vite-plugin-mock'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd())

  console.log('Command:', command)
  console.log('Mode:', mode)
  console.log('Mock:', env.VITE_USE_MOCK)

  return {
    base: '/ui',
    plugins: [
      vue(),
      // setup语法糖增强
      VueSetupExtend(),
      // 复制URL
      ServerUrlCopy(),
      // Mock
      viteMockServe({
        enable: env.VITE_USE_MOCK === 'true',
        logger: true
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      host: true,
      // open: true,
      port: 5173,
      proxy: {
        '/api': {
          target: 'http://localhost:8081',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }
  }
})
